package com.lili.collection.core.runner.stage;

import com.lili.collection.core.event.StageEvent;
import com.lili.collection.core.event.StageEventListener;
import com.lili.collection.core.runner.StageRunner;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 阶段Runner抽象
 *
 * @author lili
 * @date 2023/3/7 11:20
 */
@Slf4j
public abstract class AbstractStageRunner implements StageRunner {

    protected List<StageEventListener> stageEventListener;

    @Override
    public synchronized void registerStageEventListener(StageEventListener listener) {
        ensureStageEventListener();
        this.stageEventListener.add(listener);
    }

    @Override
    public void triggerStageEvent(StageEvent stageEvent, StageType stageType, Object params) {
        this.stageEventListener.forEach(r -> r.onStageEventTrigger(stageEvent, this.getStageType(), params));
    }

    private void ensureStageEventListener() {
        if (this.stageEventListener == null)
            this.stageEventListener = new ArrayList<>();
    }
}